package jdklearning.object;


import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author shenenlu 2021年05月07日 下午18:08:24
 */
public class hashCodeLearning {


    public static void main(String[] args) {
        Point p1 = new Point(100, 200);
        Point p2 = new Point(100, 200);
        System.out.println(p1.equals(p2));
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
        Set<Point> set = new HashSet<>();
        set.add(p1);
        set.add(p2);
        System.out.println(set);
    }

    static class Point{
        int x,y;

        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        @Override
        public int hashCode() {
            //100 200
//        return Integer.parseInt(x+" "+y);
            return Objects.hash(x,y);
        }
        @Override
        public boolean equals(Object other) {
            if (other instanceof Point) {
                Point otherPoint = (Point) other;
                return this.x == otherPoint.x && this.y == otherPoint.y;
            }
            return false;
        }

        @Override
        public String toString() {
            return "("+ x + ","+ y +")";
        }
    }

}